package org.example.examsystem.controller;


import org.example.examsystem.entity.Course;
import org.example.examsystem.entity.PjClass;
import org.example.examsystem.service.CourseService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/courses")
public class CourseController {

    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }

    @GetMapping
    public Page<Course> getCourses(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size) {
        Pageable pageable = PageRequest.of(page, size);
        return courseService.getCourses(pageable);
    }

    @PostMapping
    public void addCourse(@RequestBody Course course) {
        courseService.addCourse(course);
    }

    @PutMapping("/{cno}")
    public void updateCourse(@PathVariable Integer cno, @RequestBody Course course) {
        course.setCno(cno);
        courseService.updateCourse(course);
    }

    @DeleteMapping("/{cno}")
    public void deleteCourse(@PathVariable Integer cno) {
        courseService.deleteCourse(cno);
    }

    @GetMapping("/name/{courseName}")
    public Course getCourseByName(@PathVariable String courseName) {
        return courseService.getCourseByName(courseName);
    }


    @GetMapping("/all")
    public List<Course> getCourseList() {
        return courseService.getCourseList();
    }
}